package com.jspgou.cms.action.front;

import static com.jspgou.cms.Constants.MEMBER_SYS;

import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jspgou.cms.entity.Collect;
import com.jspgou.cms.entity.ShopMember;
import com.jspgou.cms.manager.CollectMng;
import com.jspgou.cms.web.CmsUtils;
import com.jspgou.cms.web.ShopFrontHelper;
import com.jspgou.cms.web.SiteUtils;
import com.jspgou.common.web.ResponseUtils;
import com.jspgou.common.web.springmvc.MessageResolver;
import com.jspgou.core.entity.Website;

@Controller
public class CollectAct {
	private static final Logger log = LoggerFactory.getLogger(CollectAct.class);
	
	//收藏夹
	private static final String MY_COLLECT = "tpl.mycollect";

	@RequestMapping(value = "/collect/add_to_collect.jspx")
	public String addToCollect(Long productId,Long productFashId, Boolean isAdd,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) throws JSONException {
		ShopMember member=CmsUtils.getMember(request);
		if (member == null) {
			ResponseUtils.renderJson(response, new JSONObject()
					.put("status", 0).toString());
			return null;
		}
		Collect collect =null;
		if(productFashId==null){
//			List<Collect> clist=manager.findByProductId(productId);
			//解决我的收藏夹不能正常显示
			List<Collect> clist=manager.getList(productId,member.getId());
			if (clist.size()>0) {
				ResponseUtils.renderJson(response, new JSONObject()
						.put("status", 2).toString());
				return null;
			}
			collect = manager.save(member,productId,null);
		}else{
			collect = manager.findByProductFashionId(productFashId);
				if (collect != null) {
					ResponseUtils.renderJson(response, new JSONObject()
							.put("status", 2).toString());
					return null;
				}
				collect = manager.save(member,productId,productFashId);
		}
		ResponseUtils.renderJson(response, new JSONObject().put("status", 1)
				.toString());
		return null;
	}

	@RequestMapping(value = "/collect/mycollect*.jspx")
	public String myCollect(String cl,HttpServletRequest request,ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		if(member == null){
			return "";
		}
		model.addAttribute("historyProductIds", "");
		model.addAttribute("cl", cl);
		Cookie[] cookie = request.getCookies();
		for(int i=0;i<cookie.length;i++){
			if(cookie[i].getName().equals("shop_record")){
				String str = cookie[i].getValue();
				model.addAttribute("historyProductIds", str);
				break;
			}
		}
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(MEMBER_SYS, MessageResolver.getMessage(request,
				MY_COLLECT), request);
				
	}
	
	@RequestMapping(value = "/collect/delCollect.jspx")
	public String delCollect(Integer[] collectIds,HttpServletResponse response,ModelMap model){
		manager.deleteByIds(collectIds);
		ResponseUtils.renderJson(response, "删除成功!");
		return null;
	}

	@Autowired
	private CollectMng manager;
}
